<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/3/2 0002
 * Time: 15:43
 */

namespace app\common\helper;

class RandStr {
    /**
     * 返回随机字符串
     *
     * @param int $length
     * @param string $char
     * @return bool|string
     */
    public static function str_rand($length = 32, $char = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') {
        if(!is_int($length) || $length < 0) {
            return false;
        }

        $string = '';
        for($i = $length; $i > 0; $i--) {
            $string .= $char[mt_rand(0, strlen($char) - 1)];
        }
        return $string;
    }

    /**
     * 返回唯一标识
     *
     * @return string
     */
    public static function str_unique() {
        return md5(uniqid(microtime(true),true));
    }
}